##########################################
## Copyright 2012-2013 Ceruti Francesco & contributors
##
## This file is part of LiSP (Linux Show Player).
##########################################

from PyQt4 import QtCore, QtGui


class QClickSlider(QtGui.QSlider):

    sliderJumped = QtCore.pyqtSignal(int)

    def mousePressEvent(self, e):
        opt = QtGui.QStyleOptionSlider()
        self.initStyleOption(opt)
        sr = self.style().subControlRect(QtGui.QStyle.CC_Slider, opt, QtGui.QStyle.SC_SliderHandle)
        if(e.button() == QtCore.Qt.LeftButton and not sr.contains(e.pos())):
            if (self.orientation() == QtCore.Qt.Vertical):
                value = self.minimum() + ((self.maximum() - self.minimum()) * (self.height() - e.y())) / self.height()
            else:
                value = self.minimum() + ((self.maximum() - self.minimum()) * e.x()) / self.width()
            if(self.value() != value):
                self.setValue(value)
                self.sliderJumped.emit(self.value())
                e.accept()
            else:
                e.ignore()
        else:
            super().mousePressEvent(e)
